import React from "react";
import { BackHandler } from "react-native";
import RootSiblings from "react-native-root-siblings";
import ImageViewer from "react-native-image-zoom-viewer";
import { SCREEN_HEIGHT } from "./constant";


type Options = {
    imageUrls: string[],
    success?: Function,
    fail?: Function,
    complete?: Function,
    current:string
}
export function previewImages(obj: Options) {
    const { imageUrls,current, success, fail, complete } = obj || {};

    let sibling;
    function backhandler() {
        onDestory();
        return true;
    }
    function onDestory() {
        BackHandler.removeEventListener("hardwareBackPress", backhandler);
        sibling === null || sibling === void 0 ? void 0 : sibling.destroy();
        sibling = undefined;
    }
    function onSuccess() {
        const rsp = { errMsg: "previewImage:ok" };
        onDestory();
        success === null || success === void 0 ? void 0 : success(rsp);
        complete === null || complete === void 0 ? void 0 : complete(rsp);
    }
    function onFail(e) {
        onDestory();
        fail === null || fail === void 0 ? void 0 : fail(Object.assign({ errMsg: "err" }, e));
        complete === null || complete === void 0 ? void 0 : complete("err", ...e);
    }

    // UIManager.measure(Number(triggerTarget.id), (x, y, width, height, pageX, pageY) => {
    //     console.log(pageX, pageY);
    //     const clickElementAnchor = { x: width / 2 + pageX, y: height / 2 + pageY };
    //     const initialSize = { width, height };
    
    // });

    try {
        sibling = new RootSiblings(React.createElement(ImageViewer, { style:{ position:"absolute", width:"100%",height:SCREEN_HEIGHT }, imageUrls: imageUrls.map(item=>{
            return { url:item };
        }), show: true, index:imageUrls.indexOf(current),enableSwipeDown:true,enableImageZoom:true,saveToLocalByLongPress:false }));
        BackHandler.addEventListener("hardwareBackPress", backhandler);
    }
    catch (e) {
        onFail(e);
    }

    // const clickElementAnchor = 


}